/**
 * 自定义扩展模块，用于合并多行相同列
 * 一、用法(见fan-pai-mian.html)：
 * 1.需要在lay-config配置文件中配置该扩展模块，并在需要使用的页面中引入该模块;
 * 2.页面的table.render中，在需要合并的列增加属性merge:true,合并参考列添加属性refer:true，
 * 3.页面的table.render中，在done函数下增加tableMerge.merge(this);
 * 二、使用该模块明后，建议不要开启列排序
 * date:2023/12/13
 * version:1.0
 */
layui.define(['layer'], function (exports) {
    var $ = layui.jquery;

    var tableMerge = {
        merge: function (myTable) {
            var tableBox = $(myTable.elem).next().children('.layui-table-box'),
                $main = $(tableBox
                    .children('.layui-table-body')
                    .children('table').children('tbody').children('tr').toArray().reverse()),
                $fixLeft = $(tableBox
                    .children('.layui-table-fixed-l')
                    .children('.layui-table-body')
                    .children('table').children('tbody').children('tr').toArray().reverse()),
                $fixRight = $(tableBox
                    .children('.layui-table-fixed-r')
                    .children('.layui-table-body')
                    .children('table').children('tbody').children('tr').toArray().reverse()),
                cols = myTable.cols[0],
                mergeRecord = {},
                referRecord = {};

            //遍历字段名，查找需要合并的字段及参考合并的字段
            for (let i = 0; i < cols.length; i++) {
                var item3 = cols[i],
                    field = item3.field;
                //找出需要合并的字段
                if (item3.merge) {
                    var mergeField = [field];
                    if (item3.merge !== true) {
                        if (typeof item3.merge == 'string') {
                            mergeField = [item3.merge]
                        } else {
                            mergeField = item3.merge
                        }
                    }
                    mergeRecord[i] = {
                        mergeField: mergeField,
                        rowspan: 1
                    }
                    //找出需要参考合并的字段
                    if (item3.refer) {
                        var referField = [field];
                        referRecord[i] = {
                            referField: referField
                        }
                    }
                }
            }

            //遍历每行数据，给每列增加合并行属性
            $main.each(function (i) {
                for (var item in mergeRecord) {
                    if (i == $main.length - 1 || isMaster(i, item)) {
                        $(this)
                            .children('[data-key$="-' + item + '"]')
                            .attr('rowspan', mergeRecord[item].rowspan)
                            .css('position', 'static');
                        $fixLeft.eq(i)
                            .children('[data-key$="-' + item + '"]')
                            .attr('rowspan', mergeRecord[item].rowspan)
                            .css('position', 'static');
                        $fixRight.eq(i)
                            .children('[data-key$="-' + item + '"]')
                            .attr('rowspan', mergeRecord[item].rowspan)
                            .css('position', 'static');
                        mergeRecord[item].rowspan = 1;
                    } else {
                        $(this)
                            .children('[data-key$="-' + item + '"]')
                            .remove();
                        $fixLeft.eq(i)
                            .children('[data-key$="-' + item + '"]')
                            .remove();
                        $fixRight.eq(i)
                            .children('[data-key$="-' + item + '"]')
                            .remove();
                        mergeRecord[item].rowspan += 1;
                    }
                }
            })

            //判断是否需要合并行
            function isMaster(index, item) {
                var mergeField = mergeRecord[item].mergeField;
                var dataLength = layui.table.cache[myTable.id].length;
                for (var i = 0; i < mergeField.length; i++) {
                    //最后一行和最后第二行相同列值不同，则返回true
                    if (layui.table.cache[myTable.id][dataLength - 2 - index][mergeField[i]] !==
                        layui.table.cache[myTable.id][dataLength - 1 - index][mergeField[i]]) {
                        return true;
                    }else {
                        //最后一行和最后第二行相同列值相同，且参考列不同，则返回true
                        for (var item1 in referRecord) {
                            if (layui.table.cache[myTable.id][dataLength - 2 - index][referRecord[item1].referField] !==
                                layui.table.cache[myTable.id][dataLength - 1 - index][referRecord[item1].referField]){
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
        }
    };

    exports('tableMerge', tableMerge);
});